import axios from 'axios'
import { defineStore } from 'pinia'
export const useManageStore = defineStore('manage', {
    state: () => {
        return {
            page: 'Home'
        }
    },
    actions: {
        create(title, introduction) {
            return new Promise((resolve, reject) => {
                axios
                    .post(
                        'api/project-manage/create/',
                        JSON.stringify({
                            title,
                            introduction
                        }),
                        {
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }
                    )
                    .then((response) => {
                        resolve(response.data)
                    })
                    .catch((error) => {
                        reject(error)
                        console.log(error)
                    })
            })
        },
        getProject(showType) {
            return new Promise((resolve, reject) => {
                axios
                    .get(
                        `api/project-manage/get-projects/?showType=${showType}`
                    )
                    .then((response) => {
                        resolve(response.data)
                    })
                    .catch((error) => {
                        reject(error)
                        console.log(error)
                    })
            })
        },
        collect(project_id) {
            return new Promise((resolve, reject) => {
                axios
                    .put(
                        'api/project-manage/collect/',
                        JSON.stringify({
                            project_id
                        }),
                        {
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }
                    )
                    .then((response) => {
                        resolve(response.data)
                    })
                    .catch((error) => {
                        reject(error)
                        console.log(error)
                    })
            })
        },
        delete(project_id, delOrRecover) {
            return new Promise((resolve, reject) => {
                axios
                    .post(
                        'api/project-manage/delete/',
                        JSON.stringify({
                            project_id,
                            delOrRecover
                        }),
                        {
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }
                    )
                    .then((response) => {
                        resolve(response.data)
                    })
                    .catch((error) => {
                        reject(error)
                        console.log(error)
                    })
            })
        },
        modify(project_id, title, introduction) {
            return new Promise((resolve, reject) => {
                axios
                    .put(
                        'api/project-manage/modify/',
                        JSON.stringify({
                            project_id,
                            title,
                            introduction
                        }),
                        {
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        }
                    )
                    .then((response) => {
                        resolve(response.data)
                    })
                    .catch((error) => {
                        reject(error)
                        console.log(error)
                    })
            })
        },
        search(searchContent, searchType) {
            return new Promise((resolve, reject) => {
                axios
                    .get(
                        `api/project-manage/search/?searchContent=${searchContent}&searchType=${searchType}`
                    )
                    .then((response) => {
                        resolve(response.data)
                    })
                    .catch((error) => {
                        reject(error)
                        console.log(error)
                    })
            })
        }
    }
})
